package com.ssm.service;

import com.alibaba.fastjson.JSONObject;
import com.ssm.dto.BaoqiStateDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author lvshiwei
 * @description 曝气业务类
 * @date 2021-10-18
 */
@Service
@Slf4j
public class BaoqiServiceImpl {

    //引入http请求远程接口服务
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 传送曝气状态
     * @param equipId 设备id
     * @param content 报文
     */
    public void sendToBqServer(int equipId, String content) {
        BaoqiStateDto dto = new BaoqiStateDto();
        dto.setNumberID(equipId);
        dto.setRes(content);
        String now = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        dto.setTime(Long.parseLong(now));
        String res =
                restTemplate.postForObject("http://112.126.97.123:8080/suzhou_baoqi-1.0-SNAPSHOT/baoqi/state",
                        dto, String.class);
        JSONObject reJson = JSONObject.parseObject(res);
        int code = reJson.getIntValue("code");
        if (code != 200) {
            log.error("传送曝气状态失败");
        }
    }

}
